package com.jiayou.im.service.impl

import cn.hutool.core.util.RandomUtil
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.jiayou.im.entity.User
import com.jiayou.im.mapper.UserMapper
import com.jiayou.im.service.IUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.mail.javamail.JavaMailSenderImpl
import org.springframework.mail.javamail.MimeMessageHelper
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit

/**
 *
 *
 * 服务实现类
 *
 *
 * @author lishuang
 * @since 2020-11-20
 */
@Service
open class UserServiceImpl : ServiceImpl<UserMapper?, User?>(), IUserService {

    @Autowired
    private lateinit var javaMailSenderImpl: JavaMailSenderImpl

    @Autowired
    private lateinit var stringRedisTemplate: StringRedisTemplate

    @Async
    open fun sendEmail(email: String) {
        val createMimeMessage = javaMailSenderImpl.createMimeMessage()
        val mimeMessageHelper = MimeMessageHelper(createMimeMessage, true)
        mimeMessageHelper.setFrom("1416360754@qq.com")
        mimeMessageHelper.setTo(email)
        mimeMessageHelper.setSubject("欢迎加入IM！")
        val code = RandomUtil.randomInt(10000, 999999)
        stringRedisTemplate.opsForValue().set(email, "{id:$email,code:$code}", 5, TimeUnit.MINUTES)
        mimeMessageHelper.setText("您的IM验证码是：${code},请在5分钟内完成验证！")
        javaMailSenderImpl.send(createMimeMessage)
    }

}

